using System;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using Common;
using Common.Enum;
using CustomChart;
using Infragistics.UltraChart.Data.Series;
using Infragistics.UltraChart.Resources.Appearance;
using SExperiment;
using SProject.Paramter;
using SWellPlate;

namespace MainView.MainControls.Chart
{
	public class AMPCurveChart : RawCurveChart
	{
		private const string KeyThreshold = "Threshold";

		public bool CanThreshold = true;

		protected override string GetToolTipString(ChartData chartData)
		{
			if (IsPcrResult)
			{
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.AppendLine(Utility.GetMessage("sample_index") + Utility.GetMessage("Colon") + chartData.WellCell.Well.Sample.PatientInfo.ID);
				stringBuilder.AppendLine(Utility.GetMessage("Target") + Utility.GetMessage("Colon") + chartData.SampleTargetItem.TargetName);
				stringBuilder.AppendLine(Utility.GetMessage("Channel") + Utility.GetMessage("Colon") + chartData.SampleTargetItem.ChannelNo);
				stringBuilder.AppendLine(Utility.GetMessage("Type") + Utility.GetMessage("Colon") + EnumManagement.GetSampleTypeName(chartData.SampleTargetItem.SampleType));
				stringBuilder.AppendLine(Utility.GetMessage("Ct") + Utility.GetMessage("Colon") + chartData.SampleTargetItem.Result.CtValue);
				stringBuilder.AppendLine(base.TitleY + Utility.GetMessage("Colon") + "#VALX{Y}");
				return stringBuilder.ToString();
			}
			return base.GetToolTipString(chartData);
		}

		public AMPCurveChart()
		{
			base.Margin = new Thickness(1.0);
			base.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			base.VerticalAlignment = VerticalAlignment.Stretch;
			SetResourceReference(BaseCurveChart.CaptionProperty, "amplification_program");
			SetResourceReference(BaseCurveChart.TitleXProperty, "cycles_num");
			SetResourceReference(BaseCurveChart.TitleYProperty, "Rn");
			base.MinX = 1.0;
			base.MaxX = 40.0;
			base.MinY = 0.0;
			base.MaxY = 1.0;
			base.TitleXFormat = "0";
			base.TitleYFormat = "0.000";
		}

		public override Curve GetCurve(SampleTargetItem item)
		{
			return item.Result.AMPCurve;
		}

		protected override void CreateThresholdSeries(BasicOptionItem item, ChartData chartData)
		{
			RegisterSubsetParamterEvent(chartData);
			XYSeriesEx xYSeriesEx = _ThresholdSeriesList.Where((XYSeriesEx s) => s.Tag == item).FirstOrDefault();
			if (item != null && xYSeriesEx == null)
			{
				item.Event += base.BasicOptionItem_Event;
				double threshold = item.Threshold;
				xYSeriesEx = new XYSeriesEx();
				xYSeriesEx.Visible = false;
				xYSeriesEx.PEs.Add(new PaintElement());
				xYSeriesEx.PEs[0].Fill = GetThresholdSeriesColor(item.ChannelNo);
				xYSeriesEx.Tag = item;
				xYSeriesEx.Points.Add(new XYDataPoint(base.MinX, threshold, "", false));
				xYSeriesEx.Points.Add(new XYDataPoint(base.MaxX, threshold, "", false));
				_ThresholdSeriesList.Add(xYSeriesEx);
				base.UltraChart.AddThresholdSeries(xYSeriesEx);
			}
		}

		protected override void ResetThresholdSeriesPoint()
		{
			foreach (XYSeriesEx thresholdSeries in _ThresholdSeriesList)
			{
				XYDataPointCollection points = thresholdSeries.Points;
				if (points.Count > 1)
				{
					points[0].ValueX = base.UltraChart.GetCurrentMinX();
					points[1].ValueX = base.UltraChart.GetCurrentMaxX();
				}
			}
		}

		protected override void RefreshThresholdValue(BasicOptionItem item, string property)
		{
			if (property != "Threshold")
			{
				return;
			}
			XYSeriesEx xYSeriesEx = _ThresholdSeriesList.Where((XYSeriesEx s) => s.Tag == item).FirstOrDefault();
			if (xYSeriesEx != null)
			{
				XYDataPointCollection points = xYSeriesEx.Points;
				for (int i = 0; i < points.Count; i++)
				{
					points[i].ValueY = item.Threshold;
				}
				if (xYSeriesEx.Visible)
				{
					xYSeriesEx.Visible = false;
					xYSeriesEx.Visible = true;
				}
				RaiseItemUpdateEvent();
			}
		}

		protected override string GetThresholdToolTips(XYSeriesEx series)
		{
			if (series.Tag is BasicOptionItem)
			{
				BasicOptionItem basicOptionItem = (BasicOptionItem)series.Tag;
				return basicOptionItem.TargetName + Utility.GetMessage("Colon") + basicOptionItem.Threshold.ToString("n2");
			}
			return base.GetThresholdToolTips(series);
		}

		protected override void SetThreshold(XYSeriesEx series, int x, int y)
		{
			if (series.Tag is BasicOptionItem)
			{
				BasicOptionItem basicOptionItem = (BasicOptionItem)series.Tag;
				basicOptionItem.Threshold = base.UltraChart.GetLocationYData(y);
			}
		}

		protected override Cursor GetThresholdDragCursor(string key)
		{
			return Cursors.HSplit;
		}

		protected override bool CanThresholdChanged(BasicOptionItem item)
		{
			if (CanThreshold)
			{
				if (!item.AutoThreshold)
				{
					return _ThresholdSeriesList.Where((XYSeriesEx s) => s.Visible).Count() == 1;
				}
				return false;
			}
			return CanThreshold;
		}

		public void SetMenuthresholdCurveHidden()
		{
			foreach (ToolStripItem item in base.UltraChart.ContentMenu.Items)
			{
				if (item is ToolStripMenuItem && item.Text.Equals(Utility.GetMessage("display_threshold_curve")))
				{
					item.Visible = false;
				}
			}
		}

		protected override void InitMenu()
		{
			base.UltraChart.ContentMenu.Opening += ContentMenu_Opening;
			ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
			toolStripMenuItem.Text = Utility.GetMessage("linear_axis");
			toolStripMenuItem.Click += LinearAxis_Click;
			base.UltraChart.ContentMenu.Items.Insert(2, toolStripMenuItem);
			toolStripMenuItem = new ToolStripMenuItem();
			toolStripMenuItem.Text = Utility.GetMessage("log_axis");
			toolStripMenuItem.Click += LogAxis_Click;
			base.UltraChart.ContentMenu.Items.Insert(3, toolStripMenuItem);
			toolStripMenuItem = new ToolStripMenuItem();
			toolStripMenuItem.Text = Utility.GetMessage("display_threshold_curve");
			toolStripMenuItem.Checked = base.ThresholdVisible;
			toolStripMenuItem.CheckedChanged += item_CheckedChanged;
			toolStripMenuItem.CheckOnClick = true;
			base.UltraChart.ContentMenu.Items.Insert(4, toolStripMenuItem);
		}

		private void item_CheckedChanged(object sender, EventArgs e)
		{
			ToolStripMenuItem toolStripMenuItem = sender as ToolStripMenuItem;
			if (toolStripMenuItem != null)
			{
				base.ThresholdVisible = toolStripMenuItem.Checked;
			}
		}

		protected void ContentMenu_Opening(object sender, CancelEventArgs e)
		{
			foreach (ToolStripItem item in base.UltraChart.ContentMenu.Items)
			{
				if (item is ToolStripMenuItem)
				{
					ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)item;
					if (toolStripMenuItem.Text == Utility.GetMessage("linear_axis"))
					{
						toolStripMenuItem.Checked = base.UltraChart.IsAxisYLinear();
					}
					else if (toolStripMenuItem.Text == Utility.GetMessage("log_axis"))
					{
						toolStripMenuItem.Checked = !base.UltraChart.IsAxisYLinear();
					}
					else if (toolStripMenuItem.Text == Utility.GetMessage("display_threshold_curve"))
					{
						toolStripMenuItem.Checked = base.ThresholdVisible;
						toolStripMenuItem.Enabled = base.IsDecrypt;
					}
				}
			}
		}

		protected void LinearAxis_Click(object sender, EventArgs e)
		{
			base.UltraChart.SetAxisYLinear(true);
			SetMaxY(base.MaxY);
			SetMinY(base.MinY);
			base.RefreshAll = true;
			RefreshSeries();
			base.UltraChart.RefreshAxisYInterval();
		}

		protected void LogAxis_Click(object sender, EventArgs e)
		{
			base.UltraChart.SetAxisYLinear(false);
			SetMaxY(base.MaxY);
			SetMinY(base.MinY);
			base.RefreshAll = true;
			RefreshSeries();
			base.UltraChart.RefreshAxisYInterval();
		}
	}
}
